<?php /*
Copyright 2005 Dan Boitnott (dan@lclinux.org)

This file is part of phpTasks.

phpTasks is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

phpTasks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with phpTasks; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/ ?>
<?php require("common.php"); ?>
<?php
// dump_hash($HTTP_VARS);
if (isset($HTTP_VARS["action"])) {
        if ($HTTP_VARS["action"] == "Logout") {
		$_SESSION['sys_authd'] = false;
		$_SESSION['sys_authd_userid'] = 0;
		switch ($CONFIG["auth_method"]) {
		case "html":
			html_login("index.php");
			exit;
		default:
	        	http_logout();
          	}
        }
        if ($HTTP_VARS["action"] == "promote" || $HTTP_VARS["action"] == "demote") {
                if (!$USER["modifytasks"]) {
                        echo ("You are not authorized to perform this action!");
                        exit;
                }
                if ($HTTP_VARS["action"] == "promote")
                        $m = "-1";
                else
                        $m = "+1";
                $sql = "UPDATE atasks SET priority=(priority" . $m . ") WHERE taskid=" . $HTTP_VARS["taskid"];
                pg_exec($conn, $sql);
                redirect("index.php");
        }
} else {
	$h = date("G");
	$title = "Good ";
	if ($h < 11)
		$title .= "morning ";
	elseif ($h < 16)
		$title .= "day ";
  	elseif ($h < 18)
		$title .= "afternoon ";
  	else
		$title .= "evening ";

	switch ($USER["name"]) {
		case "Dan":
			$title .= "my liege";
   			break;
		case "Cade":
			$title .= "C-Dawg!";
			break;
   		default:
			$title .= $USER["name"];
	}

	$title .= ", Welcome to " . $CONFIG["sys_name"];
?>
<?php std_header("Task List"); ?>
<table border='0' width='100%'><tr>
	<td align='left'><h1><?=$title?></h1></td>
	<td align='right'>
		<table border='0'><tr>
			<td>
				<form action='taskedit.php' method='post'>
				<input type=hidden name='taskid' value='0'>
				<input type=submit value="New Task">
				</form>
			</td><td>
				<form action='report.php' method='post'>
				<input type="submit" value="Report">
				</form>
    			</td>
			<td>
				<form action='index.php' method='post'>
					<input type=submit name="action" value="Logout">
   				</form>
       			</td>
          	</tr>
<?php if ($USER["modifyusers"]) { ?>
		<tr align=right>
			<td colspan=3>
				<form action='userman.php' method='post'>
				<input type=submit value="Manage Users">
				</form>
			</td>
   		</tr>
<?php } ?>
		</table>
	</td>
</tr>
</table>
<p></p>
<table border='0' width='100%'><tr><td align='center'>
	<form action="index.php" method="post">
		<table border='0'><tr valign=bottom>
			<td><select name="tl_view" size=1>
				<option value="open"<?=iif($_SESSION['tl_view']=="open", " selected ", "")?>>Open</option>
				<option value="closed"<?=iif($_SESSION['tl_view']=="open", "", " selected ")?>>Completed</option>
    			</select></td>
			<?php if ($USER["viewalltasks"]) { ?>
				<td><big>Tasks for </big></td>
				<td><?php
					$extra_options["0"] = "All Users";
					std_select($conn, "tl_userid", "SELECT * FROM users ORDER BY name", "name", "userid", $_SESSION['tl_userid'], false, "1", $extra_options);
				?></td>
   			<?php } else { ?>
				<td><big>Tasks for <?=$USER["name"]?></big></td>
			<?php } ?>
			<td><input type=submit value="Update"></td>
  		</tr></table>
	</form>
</td></tr><tr>
<td align='center'>
<table border='0'>
	<tr class=std_table_header>
		<td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="taskid" || $_SESSION['tl_orderby']=="i_taskid") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="taskid","i_taskid", "taskid")?>'>Task #</a></td>
                <?php if($_SESSION['tl_view'] == "open") { ?>
		        <td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="created" || $_SESSION['tl_orderby']=="i_created") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="created","i_created", "created")?>'>Opened On</a></td>
                <?php } else { ?>
                        <td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="completed" || $_SESSION['tl_orderby']=="i_completed") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="completed","i_completed", "completed")?>'>Completed On</a></td>
                <?php } ?>
		
		<td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="due" || $_SESSION['tl_orderby']=="i_due") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="due","i_due", "due")?>'>Due On</a></td>
		   
		<td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="priority" || $_SESSION['tl_orderby']=="i_priority") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="priority","i_priority", "priority")?>'>Priority</a></td>
		<?php if ($_SESSION['tl_userid'] == 0) { ?>
			<td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="assigned_to" || $_SESSION['tl_orderby']=="i_assigned_to") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="assigned_to","i_assigned_to", "assigned_to")?>'>Assigned To</a></td>
		<?php } ?>
		<td align='center'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="assigned_by" || $_SESSION['tl_orderby']=="i_assigned_by") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="assigned_by","i_assigned_by", "assigned_by")?>'>Assigned By</a></td>
		<td align='left'><a style='text-decoration:<?=($_SESSION['tl_orderby']=="title" || $_SESSION['tl_orderby']=="i_title") ? "underline" : "none"?>' href='index.php?tl_orderby=<?=iif($_SESSION['tl_orderby']=="title","i_title", "title")?>'>Task</a></td>
	</tr>
<?php
	$sql = "SELECT * FROM task_list WHERE ";
	if ($_SESSION['tl_view'] == "open")
		$sql .= "(completed IS NULL)";
	else
		$sql .= "(completed IS NOT NULL)";
  	if ($_SESSION['tl_userid'] > 0)
		$sql .=" AND (assigned_to=" . $_SESSION['tl_userid'] . ")";
	$sql .= " ORDER BY ";
	switch ($_SESSION['tl_orderby']) {
	case "taskid":
		$sql .= "taskid";
   		break;
     	case "i_taskid":
		$sql .= "taskid DESC";
		break;
      	case "created":
                if ($_SESSION['tl_view'] == "open")
		        $sql .= "created";
                else
                        $sql .= "completed";
		break;
  	case "i_created":
                if ($_SESSION['tl_view'] == "open")
		        $sql .= "created DESC";
                else
                        $sql .= "completed DESC";
		break;
        case "completed":
                if ($_SESSION['tl_view'] == "open")
		        $sql .= "created";
                else
                        $sql .= "completed";
		break;
        case "i_completed":
                if ($_SESSION['tl_view'] == "open")
		        $sql .= "created DESC";
                else
                        $sql .= "completed DESC";
		break;
	case "due":
	  $sql .= "due, priority";
	  break;
	case "i_due":
	  $sql .= "due DESC, priority DESC";
	  break;
   	case "assigned_by":
		$sql .= "assigned_by, created";
		break;
  	case "i_assigned_by":
		$sql .= "assigned_by DESC, created";
		break;
   	case "title":
		$sql .= "title";
		break;
  	case "i_title":
		$sql .= "title DESC";
		break;
  	case "i_priority":
		$sql .= "priority DESC, created";
		break;
  	case "assigned_to":
		if ($_SESSION['tl_userid'] == 0)
			$sql .= "assigned_to_name, priority, created";
   		else
			$sql .= "priority, created";
   		break;
     	case "i_assigned_to":
		if ($_SESSION['tl_userid'] == 0)
			$sql .= "assigned_to_name DESC, priority, created";
   		else
			$sql .= "priority, created";
   		break;
	default:
		$sql .= "priority, created";
	}
	$res = pg_exec($conn, $sql);
	$rc = pg_numrows($res);
	if ($rc > 0) {
		for($rnum = 0; $rnum < $rc; $rnum++) {
			$r = get_row($res, $rnum);
		?>
			<tr bgcolor='<?=priority_color($r["priority"])?>'>
				<td align='center'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["taskid"]?></a></td>
				<td align='center'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["created"]?></a></td>
				<td align='center'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["due"]?></a></td>

				<td align='center'>
                                        <?php if ($USER["modifytasks"] && ($r["priority"] < 10)) { ?>
                                                <a href='index.php?action=demote&taskid=<?=$r["taskid"]?>'>-</a>
                                        <?php } ?>
                                        <span> </span>
                                        <a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["priority_name"]?></a>
                                        <span> </span>
                                        <?php if ($USER["modifytasks"] && ($r["priority"] > 1)) { ?>
                                                <a href='index.php?action=promote&taskid=<?=$r["taskid"]?>'>+</a>
                                        <?php } ?>
                                </td>
				<?php if ($_SESSION['tl_userid'] == 0) { ?>
					<td align='center'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["assigned_to_name"]?></a></td>
				<?php } ?>
				<td align='center'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["assigned_by"]?></a></td>
				<td align='left'><a href='taskedit.php?taskid=<?=$r["taskid"]?>'><?=$r["title"]?></a></td>
			</tr>
		<?php
		}
		echo ("<tr bgcolor='silver'><td colspan=" . iif($_SESSION['tl_userid']==0, 7, 6) . " align='center'>Total: " . $rc . "</td></tr>");
	} else {
		echo ("<tr bgcolor='silver'><td align='center' colspan=" . iif($_SESSION['tl_userid']==0, 7, 6) . ">No Tasks</td></tr>");
	}
?>
</table></td>
</tr>
</table>
<?php std_footer(); ?>
<?php } ?>
